/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entityprojet;

import entityprojet.exceptions.NonexistentEntityException;
import entityprojet.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author T.Ron
 */
public class BudgetcgpbJpaController implements Serializable {

    public BudgetcgpbJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("projetT_RPU");

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Budgetcgpb budgetcgpb) throws PreexistingEntityException, Exception {
        if (budgetcgpb.getBudgetcgpbPK() == null) {
            budgetcgpb.setBudgetcgpbPK(new BudgetcgpbPK());
        }
        budgetcgpb.getBudgetcgpbPK().setNumexo(budgetcgpb.getPeriode().getPeriodePK().getNumexo());
        budgetcgpb.getBudgetcgpbPK().setMois(budgetcgpb.getPeriode().getPeriodePK().getMois());
        budgetcgpb.getBudgetcgpbPK().setIdCg(budgetcgpb.getCompteg().getIdCg());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Periode periode = budgetcgpb.getPeriode();
            if (periode != null) {
                periode = em.getReference(periode.getClass(), periode.getPeriodePK());
                budgetcgpb.setPeriode(periode);
            }
            Compteg compteg = budgetcgpb.getCompteg();
            if (compteg != null) {
                compteg = em.getReference(compteg.getClass(), compteg.getIdCg());
                budgetcgpb.setCompteg(compteg);
            }
            em.persist(budgetcgpb);
            if (periode != null) {
                periode.getBudgetcgpbCollection().add(budgetcgpb);
                periode = em.merge(periode);
            }
            if (compteg != null) {
                compteg.getBudgetcgpbCollection().add(budgetcgpb);
                compteg = em.merge(compteg);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findBudgetcgpb(budgetcgpb.getBudgetcgpbPK()) != null) {
                throw new PreexistingEntityException("Budgetcgpb " + budgetcgpb + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Budgetcgpb budgetcgpb) throws NonexistentEntityException, Exception {
        budgetcgpb.getBudgetcgpbPK().setNumexo(budgetcgpb.getPeriode().getPeriodePK().getNumexo());
        budgetcgpb.getBudgetcgpbPK().setMois(budgetcgpb.getPeriode().getPeriodePK().getMois());
        budgetcgpb.getBudgetcgpbPK().setIdCg(budgetcgpb.getCompteg().getIdCg());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Budgetcgpb persistentBudgetcgpb = em.find(Budgetcgpb.class, budgetcgpb.getBudgetcgpbPK());
            Periode periodeOld = persistentBudgetcgpb.getPeriode();
            Periode periodeNew = budgetcgpb.getPeriode();
            Compteg comptegOld = persistentBudgetcgpb.getCompteg();
            Compteg comptegNew = budgetcgpb.getCompteg();
            if (periodeNew != null) {
                periodeNew = em.getReference(periodeNew.getClass(), periodeNew.getPeriodePK());
                budgetcgpb.setPeriode(periodeNew);
            }
            if (comptegNew != null) {
                comptegNew = em.getReference(comptegNew.getClass(), comptegNew.getIdCg());
                budgetcgpb.setCompteg(comptegNew);
            }
            budgetcgpb = em.merge(budgetcgpb);
            if (periodeOld != null && !periodeOld.equals(periodeNew)) {
                periodeOld.getBudgetcgpbCollection().remove(budgetcgpb);
                periodeOld = em.merge(periodeOld);
            }
            if (periodeNew != null && !periodeNew.equals(periodeOld)) {
                periodeNew.getBudgetcgpbCollection().add(budgetcgpb);
                periodeNew = em.merge(periodeNew);
            }
            if (comptegOld != null && !comptegOld.equals(comptegNew)) {
                comptegOld.getBudgetcgpbCollection().remove(budgetcgpb);
                comptegOld = em.merge(comptegOld);
            }
            if (comptegNew != null && !comptegNew.equals(comptegOld)) {
                comptegNew.getBudgetcgpbCollection().add(budgetcgpb);
                comptegNew = em.merge(comptegNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                BudgetcgpbPK id = budgetcgpb.getBudgetcgpbPK();
                if (findBudgetcgpb(id) == null) {
                    throw new NonexistentEntityException("The budgetcgpb with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(BudgetcgpbPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Budgetcgpb budgetcgpb;
            try {
                budgetcgpb = em.getReference(Budgetcgpb.class, id);
                budgetcgpb.getBudgetcgpbPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The budgetcgpb with id " + id + " no longer exists.", enfe);
            }
            Periode periode = budgetcgpb.getPeriode();
            if (periode != null) {
                periode.getBudgetcgpbCollection().remove(budgetcgpb);
                periode = em.merge(periode);
            }
            Compteg compteg = budgetcgpb.getCompteg();
            if (compteg != null) {
                compteg.getBudgetcgpbCollection().remove(budgetcgpb);
                compteg = em.merge(compteg);
            }
            em.remove(budgetcgpb);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Budgetcgpb> findBudgetcgpbEntities() {
        return findBudgetcgpbEntities(true, -1, -1);
    }

    public List<Budgetcgpb> findBudgetcgpbEntities(int maxResults, int firstResult) {
        return findBudgetcgpbEntities(false, maxResults, firstResult);
    }

    private List<Budgetcgpb> findBudgetcgpbEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Budgetcgpb.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Budgetcgpb findBudgetcgpb(BudgetcgpbPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Budgetcgpb.class, id);
        } finally {
            em.close();
        }
    }

    public int getBudgetcgpbCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Budgetcgpb> rt = cq.from(Budgetcgpb.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
